###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# ControlPanel/GeneralOptions.pm.pl -> GeneralOptions module                  #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package CP::GeneralOptions;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Error.pm.pl";
require "Standard.pm.pl";
require "Authenticate.pm.pl";
require "Variables.pm.pl";

use strict;

require "ControlPanel/Output/GeneralOptions.pm.pl";

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
  my %in = (DB => undef, ERROR => "", @_);

  #----------------------------------------------------------------------#
  # Authenticating...                                                    #
  
  &Authenticate::CP(DB => $in{'DB'}, ACTION => "GENERALOPTIONS");

  #----------------------------------------------------------------------#
  # Getting data...                                                      #

  my ($Skins, $SkinsIndex) = $in{'DB'}->Query(
    TABLE   => "Skins",
    SORT    => "DESCRIPTION",
    BY      => "A-Z"
  );

  my ($Categories, $CategoriesIndex) = $in{'DB'}->Query(
    TABLE   => "Categories",
    SORT    => "FULL_TITLE",
    BY      => "A-Z"
  );

  my %INPUT;
  
  $INPUT{'SKINS'}      = $Skins;
  $INPUT{'CATEGORIES'} = $Categories;

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::CP::GeneralOptions->new();

  &Standard::PrintHTMLHeader();
  print $Skin->show(input => \%INPUT, error => $in{'ERROR'});
  
  return 1;
}

###############################################################################
# do subroutine
sub do {
  my $self = shift;
  my %in = (DB => undef, @_);

  #----------------------------------------------------------------------#
  # Authenticating...                                                    #
  
  &Authenticate::CP(DB => $in{'DB'}, ACTION => "GENERALOPTIONS");

  #----------------------------------------------------------------------#
  # Checking fields...                                                   #

  my @Fields = (
    { name => "SITE_TITLE"                , required => 1 },
    { name => "SITE_URL"                  , required => 1 },
    { name => "LINKS_TITLE"               , required => 1 },
    { name => "LINKS_DESCRIPTION"         , required => 1 },
    { name => "SKIN"                      , required => 1 },
    { name => "MAIL_FUNCTIONS"            , required => 1 },
    { name => "NOTIFY_ADMIN_OF_ADD"       , required => 1 },
    { name => "NOTIFY_ADMIN_OF_MOD"       , required => 1 },
    { name => "NOTIFY_ADMIN_OF_DEAD_LINK" , required => 1 },
    { name => "NOTIFY_ADMIN_OF_REMOVE"    , required => 1 },
    { name => "NOTIFY_USER_OF_ADD"        , required => 1 },
    { name => "NOTIFY_USER_OF_MOD"        , required => 1 },
    { name => "HTML_IN_ADMIN_EMAILS"      , required => 1 },
    { name => "HTML_IN_USER_EMAILS"       , required => 1 },
    { name => "VALIDATE_NEW_RESOURCES"    , required => 1 },
    { name => "VALIDATE_MOD_RESOURCES"    , required => 1 },
    { name => "VALIDATE_REM_RESOURCES"    , required => 1 },
    { name => "CONTACT_EMAIL"             , required => 1 },
    { name => "ADMIN_EMAIL"               , required => 1 },
    { name => "DATE_FORMAT"               , required => 1 },
    { name => "TIME_FORMAT"               , required => 1 },
    { name => "NEW1_CUTOFF"               , required => 1 },
    { name => "NEW2_CUTOFF"               , required => 1 },
    { name => "NEW3_CUTOFF"               , required => 1 },
    { name => "HOT1_CUTOFF"               , required => 1 },
    { name => "HOT2_CUTOFF"               , required => 1 },
    { name => "HOT3_CUTOFF"               , required => 1 },
    { name => "COOL1_CUTOFF"              , required => 1 },
    { name => "COOL2_CUTOFF"              , required => 1 },
    { name => "COOL3_CUTOFF"              , required => 1 },
    { name => "WHAT_NEW_CUTOFF"           , required => 1 },
    { name => "WHAT_NEW_NUMBER"           , required => 1 },
    { name => "WHAT_HOT_CUTOFF"           , required => 1 },
    { name => "WHAT_HOT_NUMBER"           , required => 1 },
    { name => "WHAT_COOL_CUTOFF"          , required => 1 },
    { name => "WHAT_COOL_NUMBER"          , required => 1 },
    { name => "CATEGORY_DEFAULT_SORT"     , required => 1 },
    { name => "CATEGORY_DEFAULT_BY"       , required => 1 },
    { name => "CATEGORY_DEFAULT_PERPAGE"  , required => 1 },
    { name => "WHAT_NEW_SORT_FIELD"       , required => 1 },
    { name => "WHAT_NEW_SORT_BY"          , required => 1 },
    { name => "WHAT_HOT_SORT_FIELD"       , required => 1 },
    { name => "WHAT_HOT_SORT_BY"          , required => 1 },
    { name => "WHAT_COOL_SORT_FIELD"      , required => 1 },
    { name => "WHAT_COOL_SORT_BY"         , required => 1 },
    { name => "BAN_IPS"                   , required => 0, array => 1 },
    { name => "BAN_URLS"                  , required => 0, array => 1 },
    { name => "BAN_WORDS"                 , required => 0, array => 1 },
    { name => "IMAGE_UPLOAD"              , required => 1 },
    { name => "IMAGE_URL"                 , required => 1 },
    { name => "UPLOAD_MAXSIZE"            , required => 0 },
    { name => "UPLOAD_MAXWIDTH"           , required => 0 },
    { name => "UPLOAD_MAXHEIGHT"          , required => 0 },
    { name => "UPLOAD_EXTENSIONS"         , required => 0, array => 1 },
    { name => "HTML_IN_REVIEWS"           , required => 1 },
    { name => "CHECK_DUPLICATE_PROTOCOL"  , required => 0 },
    { name => "CHECK_DUPLICATE_HOST"      , required => 0 },
    { name => "CHECK_DUPLICATE_PORT"      , required => 0 },
    { name => "CHECK_DUPLICATE_DIRECTORY" , required => 0 },
    { name => "CHECK_DUPLICATE_FILENAME"  , required => 0 },
    { name => "CHECK_DUPLICATE_EXTENSION" , required => 0 },
    { name => "CHECK_DUPLICATE_QUERY"     , required => 0 },
    { name => "CHECK_DUPLICATE_ACTION"    , required => 0 },

    { name => "SIZE_TITLE"                , required => 1 },
    { name => "SIZE_DESCRIPTION"          , required => 1 },
    { name => "SIZE_KEYWORDS"             , required => 1 },
    
    { name => "SEARCH_DEFAULT_FIELDS"     , required => 1, realarray => 1 },
    { name => "SEARCH_DEFAULT_CASE"       , required => 1 },
    { name => "SEARCH_DEFAULT_WHOLE"      , required => 0 },
    { name => "SEARCH_DEFAULT_PERPAGE"    , required => 1 },
    
    { name => "RANDOM_MIN_HITS"           , required => 0 },
    { name => "RANDOM_MIN_RATING"         , required => 0 },
    { name => "RANDOM_CATEGORIES"         , required => 0, realarray => 1 },
    
    { name => "FEATURED"                  , required => 1 },
    { name => "FEATURED_MIN_HITS"         , required => 0 },
    { name => "FEATURED_MIN_RATING"       , required => 0 },
    { name => "FEATURED_CATEGORIES"       , required => 0, realarray => 1 }
  );

  my (%RECORD, @Error);

  foreach my $field (@Fields) {
    if ($field->{'required'} && $SL::QUERY{'FORM_'.$field->{'name'}} eq "") {
      push(@Error, "MISSING-".$field->{'name'});
    } elsif ($field->{'array'}) {
      @{ $RECORD{$field->{'name'}} } = split(/\|/, $SL::QUERY{'FORM_'.$field->{'name'}});
    } elsif ($field->{'realarray'}) {
      @{ $RECORD{$field->{'name'}} } = $SL::CGI->param('FORM_'.$field->{'name'});
    } else {
      $RECORD{ $field->{'name'} } = $SL::QUERY{'FORM_'.$field->{'name'}};
    }
  }

  my @CategoryExtra = $SL::CGI->param('FORM_CATEGORY_EXTRA');
  $RECORD{'CATEGORY_EXTRA'} = [];
  foreach my $field (@CategoryExtra) {
    my ($id, $description) = split(/\|\^\|/, $field);
    push(@{ $RECORD{'CATEGORY_EXTRA'} }, { ID => $id, DESCRIPTION => $description });
  }

  if (scalar(@Error) >= 1) {
    $self->show(DB => $in{'DB'}, ERROR => \@Error);
    return 1;
  }

  #----------------------------------------------------------------------#
  # Updating data...                                                     #

  my $Variables = Variables->new();
  
  $Variables->Update(
    FILE      => "$SL::PATH/Private/Variables/General.pm.pl",
    PACKAGE   => "General",
    VARIABLE  => "GENERAL",
    VALUES    => \%RECORD
  ) || &Error::Error("CP", MESSAGE => "Error updating variable file. $Variables->{'ERROR'}");
  
  (
    $in{'DB'}->ModifyTable(
      TABLE   => "Resources",
      VALUE   => {
        TITLE       => $RECORD{'SIZE_TITLE'},
        DESCRIPTION => $RECORD{'SIZE_DESCRIPTION'},
        KEYWORDS    => $RECORD{'SIZE_KEYWORDS'}
      }
    )
  ) || &Error::Error("CP", MESSAGE => "Error modifying table. $in{'DB'}->{'ERROR'}");
  
  my %INPUT;
  
  $INPUT{'RECORD'} = \%RECORD;
  
  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::CP::GeneralOptions->new();

  &Standard::PrintHTMLHeader();
  print $Skin->do(input => \%INPUT);
  
  return 1;
}

1;